package com.skyworth.web.common.controller;

import com.skyworth.utils.FileManager;
import com.skyworth.web.common.domain.AttachmentModel;
import com.skyworth.web.common.repository.AttachmentRepository;
import com.skyworth.web.common.service.AttachmentService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Paths;
import java.util.Map;


@Controller
public class UploadController extends BaseController {
    Logger LOG = LoggerFactory.getLogger(UploadController.class);

    //The Environment object will be used to read parameters from the application.properties configuration file
    @Autowired
    private Environment env;

    @Autowired
    private UserService userService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private AttachmentRepository attachRepository;

    @RequestMapping(value = {"/upload"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> upload(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request)
    {
        Map<String, Object> responseMap = responseMap();

        long userId = 0;
        UserModel user = currentUser();
        if(null != user){
            userId = user.getId();
        }
        String baseUploadDir = env.getProperty("fanfou.paths.uploadedFiles");
        String baseUploadUrl = env.getProperty("fanfou.paths.uploadedUrls");

        if(!file.isEmpty()) {
            String uploadDir = FileManager.todayUploadFolder(baseUploadDir);
            String uploadFilePath = Paths.get(uploadDir, FileManager.uuidFilename(file.getOriginalFilename())).toString();
            LOG.info("upload file " + uploadFilePath);

            boolean ret = FileManager.upload(file, uploadFilePath);
            if (ret == true) {
                String relativePath = baseUploadUrl + uploadFilePath.replace(baseUploadDir, "");
                AttachmentModel item = new AttachmentModel();

                item.name = file.getOriginalFilename();
                item.url = relativePath;
                item.userId = userId;

                attachRepository.save(item);

                responseMap.put("message", "上传成功");
                responseMap.put("data", item);
            }else {
                responseMap.put("success", false);
                responseMap.put("message", "上传失败");
            }
        }

        return responseMap;
    }

}
